//
//  WmcShareWatermarkFooterView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/8/10.
//

import UIKit

public extension WmcButtonItemType {
    enum ShareWMFooter {
        /// 微信
        case wechat

        /// 朋友圈
        case friendsCircle

        /// 自定义
        case custom(view: UIView)
    }
}

// MARK: - [WmcShareWatermarkFooterView]

class WmcShareWatermarkFooterView: WmcBaseView {
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_lineColor
        return view
    }()

    lazy var titleLabel: UILabel = {
        let label = UILabel.YP.Create(text: "分享水印给同事",
                                      font: 16.yp.uiFont(weight: .bold),
                                      color: UIColor.wmc_grayH2_323233,
                                      alignment: .left)
        return label
    }()

    lazy var starkView: UIStackView = {
        let view = UIStackView()
        view.alignment = .fill
        view.distribution = .fill
        view.axis = .horizontal
        view.spacing = 36
        return view
    }()

    lazy var wechatItem: WmcShareWatermarkItem = {
        let item = createItem("微信", image: "wechat_icon_color".wmc_image)
        return item
    }()

    lazy var friendsCircleItem: WmcShareWatermarkItem = {
        let item = createItem("朋友圈", image: "friends_circle_color".wmc_image)
        return item
    }()

    lazy var desLabel: UILabel = {
        let label = UILabel.YP.Create(text: "邀请同事一起使用统一水印",
                                      font: 14.wmc_font,
                                      color: UIColor.wmc_grayH8_8A8A99,
                                      alignment: .left)
        return label
    }()
    
    func createItem(_ title: String, image: UIImage) -> WmcShareWatermarkItem {
        let item = WmcShareWatermarkItem()
        item.title = title
        item.textColor = .wmc_grayH2_323233
        item.font = 14.wmc_font
        item.textAlignment = .center
        item.imageView.image = image
        return item
    }
    

    override func makeUI() {
        addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(8)
        }

        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom)
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.height.equalTo(54)
        }

        addSubview(starkView)
        starkView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.equalTo(titleLabel)
            make.height.equalTo(80)
            make.right.lessThanOrEqualToSuperview().offset(-Wmc_standardEdge.right)
            make.width.equalTo(100)
        }

        addSubview(desLabel)
        desLabel.snp.makeConstraints { make in
            make.top.equalTo(starkView.snp.bottom).offset(21)
            make.left.right.equalTo(titleLabel)
        }
        
        reloadItem()
    }

    func reloadItem() {
        let defaultList: [WmcButtonItemType.ShareWMFooter] = [
            .wechat, .friendsCircle,
        ]
        let views = toItemList(caseList: defaultList)
        
        let spacing = CGFloat(views.count - 1) * starkView.spacing
        let itemWidth = CGFloat(views.count) * 48.0
        let width = spacing + itemWidth
        starkView.snp.updateConstraints { make in
            make.width.equalTo(width)
        }
        starkView.yp.arrangedSubviews = views
    }

    func toItemList(caseList: [WmcButtonItemType.ShareWMFooter]) -> [UIView] {
        var views: [UIView] = []
        for cas in caseList {
            switch cas {
            case .wechat: views.append(wechatItem)
            case .friendsCircle: views.append(friendsCircleItem)
            case let .custom(view: view):
                views.append(view)
            }
        }
        return views
    }
}
